/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simpledraw;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Stack;

/**
 *
 * @author renatocardoso
 */
public class DrawingPanel extends Canvas implements MouseListener,MouseMotionListener {

  private Point sPoint = null;
  private Point ePoint = null;
  private Shapes shape = new DefaultShape();
  private Stack<Shapes> stack = new Stack<Shapes>();
  private Stack<Shapes> stackReDo = new Stack<Shapes>();
  private Shapes tmpShape = new DefaultShape();
  private Shapes tmpShape2 = new DefaultShape();

    @Override
  public void paint(Graphics g)
  {
        clearPanel(g);
        g.setColor(Color.BLACK);
        if(tmpShape != null)
            tmpShape.draw(g);
        if(tmpShape2 != null)
            tmpShape2.draw(g);
        for(int i = 0; i < stack.size(); i++){
            stack.get(i).draw(g);
      }
  }



  public void drawShape(Shapes shape)
  {
    this.shape = shape;
    if(shape instanceof TriangleShape){
        this.tmpShape = new LineShape();
        this.tmpShape2 = new LineShape();
      }
    else
        this.tmpShape = shape;
  }
  //define mouse handler
  public void mouseClicked(MouseEvent e)
  {
    if(shape instanceof TriangleShape)
    {
      shape.getList().add(e.getPoint());
      if(shape.getList().size() == 2)
          tmpShape2 = tmpShape;
      if(shape.getList().size() > 2)
      {
          stack.add(shape);
          repaint();
          shape = new TriangleShape();
          tmpShape = new LineShape();
          tmpShape2 = new LineShape();
          sPoint = null;
          ePoint = null;
      }

    }
    else if(shape instanceof PolygonShape)
    {
      shape.getList().add(e.getPoint());
    }
  }//mouseClicked


    public void mousePressed(MouseEvent e)
    {
        sPoint = e.getPoint();
    }//mousePressed

    public void mouseReleased(MouseEvent e)
    {
        ePoint = e.getPoint();
        if(shape instanceof RectangleShape || shape instanceof OvalShape || shape instanceof LineShape)
        {
          if(shape instanceof RectangleShape)
              shape = new RectangleShape();
          else if(shape instanceof OvalShape)
              shape = new OvalShape();
          else if(shape instanceof LineShape)
              shape = new LineShape();
          shape.getList().add(sPoint);
          shape.getList().add(ePoint);
          stack.add(shape);
          repaint();
          sPoint = null;
        }
    }//mouseReleased

    public void mouseEntered(MouseEvent e) {}

    public void mouseExited(MouseEvent e) {}

    public void mouseDragged(MouseEvent e) {
        ePoint = e.getPoint();
        if(shape instanceof RectangleShape || shape instanceof OvalShape || shape instanceof LineShape)
        {
            if(tmpShape instanceof RectangleShape)
              tmpShape = new RectangleShape();
          else if(tmpShape instanceof OvalShape)
              tmpShape = new OvalShape();
          else if(tmpShape instanceof LineShape)
              tmpShape = new LineShape();
          tmpShape.getList().add(sPoint);
          tmpShape.getList().add(ePoint);          
          repaint();
        }
    }

    @Override
    public void update(Graphics g) {
        paint(g);
    }

    public void mouseMoved(MouseEvent e) {
        if(sPoint != null){
            ePoint = e.getPoint();
            if(shape instanceof TriangleShape){
                tmpShape = new LineShape();
                tmpShape.getList().add(sPoint);
                tmpShape.getList().add(ePoint);
                repaint();
            }
        }        
    }

    public void clear(){
        this.shape = new DefaultShape();
        this.tmpShape = new DefaultShape();
        this.shape.getList().add(getWidth());
        this.shape.getList().add(getHeight());
        stack.add(this.shape);
        repaint();
    }

    public Stack<Shapes> getStack(){
        return this.stack;
    }

    public void undo(){
        if(stack.size() > 0){
            this.stackReDo.add(this.stack.get(stack.size()-1));
            this.stack.remove(this.stack.size()-1);
            this.tmpShape = null;
            this.tmpShape2 = null;
            repaint();
        }
    }

    public void redo(){
        if(stackReDo.size() > 0){
            this.stack.add(this.stackReDo.get(stackReDo.size()-1));
            this.stackReDo.remove(this.stackReDo.size()-1);
            this.tmpShape = null;
            this.tmpShape2 = null;
            repaint();
        }
    }

    public void clearPanel(Graphics g){
        Shapes s = new DefaultShape();
        s.getList().add(getWidth());
        s.getList().add(getHeight());
        s.draw(g);
    }

}
